package com.ldj.tim.server.consumer;

import com.ldj.tim.common.constant.MqConstants;
import com.ldj.tim.common.pojo.P2PInfo;
import com.ldj.tim.server.util.RabbitMsgUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author majinhui
 * @description
 * @create 2024/11/11 17:32
 **/
@Component
@RabbitListener(queues = MqConstants.CACHE_QUEUE)
public class Msg2RedisConsumer {

    @Autowired
    private RabbitMsgUtil rabbitMsgUtil;

    @RabbitHandler
    public void receiveMessage(Message message, P2PInfo info, Channel channel) throws IOException {
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            System.out.println("Msg2RedisConsumer 消费消息：" + info);
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            rabbitMsgUtil.retryMessage(message, info, channel, MqConstants.DLX_CACHE_ROUTING_KEY);
        }
    }

}
